package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.util.ArrayList;
import java.util.HashMap;

public class TaskManager {
    
    /**
     * Key.
     */
    private static ArrayList<String> key = new ArrayList<String>();
    
    /**
     * Task.
     */
    private static HashMap<String, Process> task = new HashMap<String, Process>();
    
    /**
     * Add a task to task manager.
     * 
     * @param key
     * @param task 
     */
    public static void addTask(String key, Task task) {
        TaskManager.key.add(key);
        TaskManager.task.put(key, task.getProcess());
    }
    
    /**
     * Get all running process.
     * 
     * @return ArrayList
     */
    public static ArrayList getAllProcess() {
        return key;
    }
    
    /**
     * Kill a task from task manager.
     * 
     * @param key 
     */
    public static void killTask(String key) {
        TaskManager.task.get(key).destroy();
        TaskManager.task.remove(key);
        TaskManager.key.remove(key);
    }
    
    /**
     * Kill all running task.
     */
    public static void killAllTask() {
        for (byte i=0; i<TaskManager.key.size(); i++) {
            TaskManager.killTask(TaskManager.key.get(i));
            TaskManager.task.remove(key.get(i));
            TaskManager.key.remove(key.get(i));
        }
    }
    
}
